package com.xxxx.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xxxx.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author Str
 * @Date 2021/9/30 14:09
 * @Description :
 */
@Controller
public class UserController {

    @RequestMapping(value = "/t1", produces = "application/json;charset=utf-8")  // produce设置json字符集
    @ResponseBody   // 这个不会走视图解析器，而是直接返回字符串
    public String test1() throws JsonProcessingException {
        //创建一个jackson的对象映射器，用来解析数据
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user = new User(1, "张三", "123", 20);
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(user);
        //由于@ResponseBody注解，这里会将str转成json格式返回；十分方便
        return str;
    }
}
